#ifndef GX_FR_EDITOR_MATERIAL_LISTVIEW_H
#define GX_FR_EDITOR_MATERIAL_LISTVIEW_H

#include "LibGL/GxUI.h"
#include "EditorMaterial.h"
#include "Windows/GxThreads.h"

namespace FinalRendering
{
	namespace Editor
	{
		using namespace GxLibBasic;

		using GxRendering::UI::Image;
		using GxRendering::UI::Label;
		using GxRendering::UI::Control;
		using GxRendering::UI::ScrollBar;

		class MaterialListView : public Control
		{
			static const int CacheSize = 100;
		public:
			struct MaterialCacheItem
			{
				EditorMaterial * material;
				int itemId;
				bool Uploaded;
				GxWinSystem::Threading::SpinLock lock;
			};
			class Item
			{
			public:
				String FileName;
				String FullFileName;
			public:
				enum ItemType
				{
					itFile, itFolder
				};
				int Id;
				ItemType Type;
				MaterialCacheItem * MaterialCache;
			};
		private:
			int cols, rows;
			int itemHeight, itemWidth;
			int thumbSize;
			int cwidth, cheight;
			int matPtr;
			String curPath;
			RefPtr<Image> imgFolder;
			RefPtr<ScrollBar> sb;
			RefPtr<Label> lbl;
			Array<MaterialCacheItem, true> materials;
			
			RefPtr<GxWinSystem::Threading::gxThread> loaderThread;
			GxWinSystem::Threading::gxLock loaderLock;
			void MouseWheel(GxRendering::UI::UI_Base *sender, GxRendering::UI::UIMouseEventArgs &e);
			void AddItem(const String & fileName, Item::ItemType type);
			void DrawContents();
			void DrawItem(Item & item, int x, int y, int absX, int absY, bool selected);
			void LoadMaterials();
			
		public:
			Array<RefPtr<Item>> items;
			GxRendering::UI::NotifyEvent OnDirectoryChanged;
			void DoubleClicked(GxRendering::UI::UI_Base * sender);
			int SelectedIndex;
			void SetDirectory(const String & path);
			String GetSelectedFileName();
			void SetThumbnailSize(int size);
			int HitTest(int x, int y);
			void DoMouseWheel();
			void DoMouseDown(int X, int Y, GxRendering::UI::SHIFTSTATE Shift);
			void DoMouseUp(int X, int Y, GxRendering::UI::SHIFTSTATE Shift);
			void DoMouseMove(int X, int Y);
			void DoKeyDown(unsigned short Key, GxRendering::UI::SHIFTSTATE Shift);
			void Draw(int absX, int absY);
			void HandleMessage(const GxRendering::UI::UI_MsgArgs * Args);
			String GetCurrentDirectory();
			void StartLoading();
			void StopLoading();
			void SizeChanged();
			MaterialListView();
			~MaterialListView();
		};
	}
}

#endif